package com.qf.controller;

import com.qf.entity.Cart;
import com.qf.entity.Goods;
import com.qf.entity.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package com.qf.controller
 * @description: ${TODO}
 * @date 2022/6/23 16:31
 */
@WebServlet(urlPatterns = "/cartservlet")
public class CartServlet extends BaseServlet {
    private static CartService cartService;
    private static GoodsService goodsService;

    @Autowired
    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @Autowired
    public void setCartService(CartService cartService) {
        this.cartService = cartService;
    }

    //添加购物车
    public String addCart(HttpServletRequest request, HttpServletResponse response){
        //(1)判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }
        //(2)获取数据
        //&goodsId=2&number=1
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number");
        if(StringUtils.isEmpty(goodsId)){
            return "redirect:/index.jsp";
        }
        int num=1;
        if(!StringUtils.isEmpty(number)){
            try {
                num=Integer.parseInt(number);
                if(num<1||num>5){
                    num=1;
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        //(4)调用
        try {
            Goods goods=goodsService.findById(Integer.parseInt(goodsId));
            //先查询购物项
            Cart cart=cartService.findByUidAndPid(user.getId(),Integer.parseInt(goodsId));
            if(cart==null) {//添加操作
                cart = new Cart(user.getId(), Integer.parseInt(goodsId), num, goods.getPrice().multiply(new BigDecimal(num)));
                cartService.add(cart);
            }else{//更新操作
                cart.setNum(cart.getNum()+num);
                cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                cartService.update(cart);
            }
            return "/cartSuccess.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","添加购物车失败:"+e.getMessage());
            return "/message.jsp";
        }
    }
    //查询购物车
    public String getCart(HttpServletRequest request,HttpServletResponse response){
        //(1)判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }

        try {
            //(3)调用方法
            List<Cart> cartList=cartService.findByUid(user.getId());
            for (Cart cart : cartList) {
                System.out.println(cart.toString());
            }
            //(4)放入域中
            request.setAttribute("cartList", cartList);
            return "/cart.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询购物车失败:"+e.getMessage());
            return "/message.jsp";
        }
    }

    //ajax操作: 增加、减少、删除商品
    public String addCartAjax(HttpServletRequest request,HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return null;
        }
        //1获取数据 &goodsId="+pid+"&number=1"
        String goodsId = request.getParameter("goodsId");
        String number = request.getParameter("number"); // 1  -1  0
        if(StringUtils.isEmpty(goodsId)){
            return null;
        }

        //3 调用方法
        if(number.equals("1")||number.equals("-1")){
            //修改
            int num=Integer.parseInt(number);// 1 或 -1
            try {
                //查询购物项
                Cart cart=cartService.findByUidAndPid(user.getId(), Integer.parseInt(goodsId));
                //查询商品
                Goods goods = goodsService.findById(Integer.parseInt(goodsId));
                if (cart != null&&goods!=null) {
                    cart.setNum(cart.getNum()+num);
                    cart.setMoney(goods.getPrice().multiply(new BigDecimal(cart.getNum())));
                    cartService.update(cart);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(number.equals("0")){
            //删除
            try {
                cartService.delete(user.getId(),Integer.parseInt(goodsId));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    //ajax操作:清空用户的购物车
    public String clearCartAjax(HttpServletRequest request,HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return null;
        }

        //2调用方法
        try {
            cartService.deleteByUid(user.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
